package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  C端购物车控制层
 */
@Api(tags = "C端购物车接口")
@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@AllArgsConstructor
public class ShoppingCartController {

    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车
     * @param shoppingCartDTO 购物车商品信息
     * @return 成功响应对象
     */
    @ApiOperation("添加购物车")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result addToCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.addToCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 清空购物车商品
     * @return 成功响应对象
     */
    @DeleteMapping(value = "/clean")
    @ApiOperation("清空购物车商品")
    public Result clean() {
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

    /**
     * 客户端查看购物车
     * @return 根据用户ID查询的购物车商品集合
     */
    @GetMapping("/list")
    @ApiOperation("客户端查看购物车")
    public Result<List<ShoppingCart>> list() {

        List<ShoppingCart> list = shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

    @PostMapping("/sub")
    @ApiOperation("减去购物车商品数量")
    public Result reduceShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("减去购物车商品数量:{}", shoppingCartDTO);
        shoppingCartService.reduceShoppingCartItem(shoppingCartDTO);
        return Result.success();
    }

}
